package qldc.pm.tienan.portlet.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import qldc.pm.qtht.dmdc.model.PhuongXa;
import qldc.pm.qtht.dmdc.model.QuanHuyen;
import qldc.pm.qtht.dmdc.service.PhuongXaLocalServiceUtil;
import qldc.pm.qtht.dmdc.service.QuanHuyenLocalServiceUtil;
import qldc.pm.qtht.utils.NguoiDungUtils;
import qldc.pm.service.utils.maphatsinh.MaPhatSinhFactory;
import qldc.pm.service.utils.maphatsinh.TenMaPhatSinh;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;

public class ResourceRequestHandler {
	public void loadQuanHuyen(ResourceRequest request, ResourceResponse response) throws IOException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long tinhIdSelected = ParamUtil.getLong(request, "tinhIdSelected");

		if (tinhIdSelected > 0) {
			List<QuanHuyen> quanHuyens = QuanHuyenLocalServiceUtil.findByTinhId(tinhIdSelected);
			JSONObject jsonObject;
			if(Validator.isNotNull(quanHuyens)) {
				for (QuanHuyen quanHuyen : quanHuyens) {
					jsonObject = JSONFactoryUtil.createJSONObject();
					jsonObject.put("quanHuyenId", quanHuyen.getQuanHuyenId());
					jsonObject.put("tenQuanHuyen", quanHuyen.getTenQuanHuyen());

					jsonArray.put(jsonObject);
				}
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}

	public void loadPhuongXa(ResourceRequest request, ResourceResponse response) throws IOException {
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long quanHuyenIdSelected = ParamUtil.getLong(request, "quanHuyenIdSelected");

		if (quanHuyenIdSelected > 0) {
			List<PhuongXa> phuongXas = PhuongXaLocalServiceUtil.findByQuanHuyenId(quanHuyenIdSelected);

			JSONObject jsonObject;
			if(Validator.isNotNull(phuongXas)) {
				for (PhuongXa phuongXa : phuongXas) {
					jsonObject = JSONFactoryUtil.createJSONObject();
					jsonObject.put("phuongXaId", phuongXa.getPhuongXaId());
					jsonObject.put("tenPhuongXa", phuongXa.getTenPhuongXa());

					jsonArray.put(jsonObject);
				}
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}
	
	public void layMaPhieuThongTinTienAn(ResourceRequest request, ResourceResponse response) throws IOException, SystemException {
		PrintWriter pw = response.getWriter();

		JSONObject jsonObj = JSONFactoryUtil.createJSONObject();

		long userId = PortalUtil.getUserId(request);

		String maTinh = "";

		String maQuanHuyen = "";

		String maPhuongXa = "";

		if (userId > 0) {
			Map<String, String> diaChiNguoiDungs = NguoiDungUtils.getDiaChiNguoiDung(userId);

			maTinh = diaChiNguoiDungs.get(NguoiDungUtils.MA_TINH);

			maQuanHuyen = diaChiNguoiDungs.get(NguoiDungUtils.MA_QUAN_HUYEN);

			maPhuongXa = diaChiNguoiDungs.get(NguoiDungUtils.MA_PHUONG_XA);
		}
		String maSoHoSoTienAnTuToaAn = MaPhatSinhFactory.getMaPhatSinh(TenMaPhatSinh.PHIEU_THONG_TIN_TIEN_AN, maTinh, maQuanHuyen, maPhuongXa);

		jsonObj.put("maSoHoSoTienAnTuToaAn", maSoHoSoTienAnTuToaAn);
		pw.print(jsonObj.toString());
		pw.flush();
	}
	
	public void layMaKetQuaYCTL(ResourceRequest request, ResourceResponse response) throws IOException, SystemException {
		PrintWriter pw = response.getWriter();

		JSONObject jsonObj = JSONFactoryUtil.createJSONObject();

		long userId = PortalUtil.getUserId(request);

		String maTinh = "";

		String maQuanHuyen = "";

		String maPhuongXa = "";

		if (userId > 0) {
			Map<String, String> diaChiNguoiDungs = NguoiDungUtils.getDiaChiNguoiDung(userId);

			maTinh = diaChiNguoiDungs.get(NguoiDungUtils.MA_TINH);

			maQuanHuyen = diaChiNguoiDungs.get(NguoiDungUtils.MA_QUAN_HUYEN);

			maPhuongXa = diaChiNguoiDungs.get(NguoiDungUtils.MA_PHUONG_XA);
		}
		String maKetQuaTrichLuc = MaPhatSinhFactory.getMaPhatSinh(TenMaPhatSinh.YEU_CAU_TRICH_LUC, maTinh, maQuanHuyen, maPhuongXa);

		jsonObj.put("maKetQuaTrichLuc", maKetQuaTrichLuc);
		pw.print(jsonObj.toString());
		pw.flush();
	}
	
	public void layMaHoSoXoaAnTich(ResourceRequest request, ResourceResponse response) throws IOException, SystemException {
		PrintWriter pw = response.getWriter();

		JSONObject jsonObj = JSONFactoryUtil.createJSONObject();

		long userId = PortalUtil.getUserId(request);

		String maTinh = "";

		String maQuanHuyen = "";

		String maPhuongXa = "";

		if (userId > 0) {
			Map<String, String> diaChiNguoiDungs = NguoiDungUtils.getDiaChiNguoiDung(userId);

			maTinh = diaChiNguoiDungs.get(NguoiDungUtils.MA_TINH);

			maQuanHuyen = diaChiNguoiDungs.get(NguoiDungUtils.MA_QUAN_HUYEN);

			maPhuongXa = diaChiNguoiDungs.get(NguoiDungUtils.MA_PHUONG_XA);
		}
		String maHoSoXoaAnTich = MaPhatSinhFactory.getMaPhatSinh(TenMaPhatSinh.HO_SO_XOA_AN_TICH, maTinh, maQuanHuyen, maPhuongXa);

		jsonObj.put("maHoSoXoaAnTich", maHoSoXoaAnTich);
		pw.print(jsonObj.toString());
		pw.flush();
	}

	public void layMaYCTL(ResourceRequest request, ResourceResponse response) throws IOException, SystemException {
		PrintWriter pw = response.getWriter();

		JSONObject jsonObj = JSONFactoryUtil.createJSONObject();

		long userId = PortalUtil.getUserId(request);

		String maTinh = "";

		String maQuanHuyen = "";

		String maPhuongXa = "";

		if (userId > 0) {
			Map<String, String> diaChiNguoiDungs = NguoiDungUtils.getDiaChiNguoiDung(userId);

			maTinh = diaChiNguoiDungs.get(NguoiDungUtils.MA_TINH);

			maQuanHuyen = diaChiNguoiDungs.get(NguoiDungUtils.MA_QUAN_HUYEN);

			maPhuongXa = diaChiNguoiDungs.get(NguoiDungUtils.MA_PHUONG_XA);
		}
		String maYeuCauTrichLuc = MaPhatSinhFactory.getMaPhatSinh(TenMaPhatSinh.YEU_CAU_TRICH_LUC, maTinh, maQuanHuyen, maPhuongXa);

		jsonObj.put("maYeuCauTrichLuc", maYeuCauTrichLuc);
		pw.print(jsonObj.toString());
		pw.flush();
	}
}
